<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务地址所对应的用户ID" prop="addressUserid">
        <el-input
          v-model="queryParams.addressUserid"
          placeholder="请输入服务地址所对应的用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务地址" prop="addressInfo">
        <el-input
          v-model="queryParams.addressInfo"
          placeholder="请输入服务地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门牌号" prop="addressDetail">
        <el-input
          v-model="queryParams.addressDetail"
          placeholder="请输入门牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="addressPhone">
        <el-input
          v-model="queryParams.addressPhone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="addressCreateBy">
        <el-input
          v-model="queryParams.addressCreateBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeAddressCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="更新者" prop="addressUpdateBy">
        <el-input
          v-model="queryParams.addressUpdateBy"
          placeholder="请输入更新者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="daterangeAddressUpdateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="删除标志" prop="addressDelFlag">
        <el-select v-model="queryParams.addressDelFlag" placeholder="请选择删除标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="默认标志" prop="addressDefaultFlag">
        <el-select v-model="queryParams.addressDefaultFlag" placeholder="请选择默认标志" clearable>
          <el-option
            v-for="dict in dict.type.service_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['service:address:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['service:address:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['service:address:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['service:address:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="addressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务地址主键ID" align="center" prop="addressId" />
      <el-table-column label="服务地址所对应的用户ID" align="center" prop="addressUserid" />
      <el-table-column label="服务地址" align="center" prop="addressInfo" />
      <el-table-column label="门牌号" align="center" prop="addressDetail" />
      <el-table-column label="联系人" align="center" prop="addressPeople" />
      <el-table-column label="手机号" align="center" prop="addressPhone" />
      <el-table-column label="创建者" align="center" prop="addressCreateBy" />
      <el-table-column label="创建时间" align="center" prop="addressCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addressCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="addressUpdateBy" />
      <el-table-column label="更新时间" align="center" prop="addressUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addressUpdateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="addressDelFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.addressDelFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="默认标志" align="center" prop="addressDefaultFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.service_yes_no" :value="scope.row.addressDefaultFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="addressRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['service:address:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:address:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务地址所对应的用户ID" prop="addressUserid">
          <el-input v-model="form.addressUserid" placeholder="请输入服务地址所对应的用户ID" />
        </el-form-item>
        <el-form-item label="服务地址" prop="addressInfo">
          <el-input v-model="form.addressInfo" placeholder="请输入服务地址" />
        </el-form-item>
        <el-form-item label="门牌号" prop="addressDetail">
          <el-input v-model="form.addressDetail" placeholder="请输入门牌号" />
        </el-form-item>
        <el-form-item label="手机号" prop="addressPhone">
          <el-input v-model="form.addressPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="创建者" prop="addressCreateBy">
          <el-input v-model="form.addressCreateBy" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="创建时间" prop="addressCreateTime">
          <el-date-picker clearable
            v-model="form.addressCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新者" prop="addressUpdateBy">
          <el-input v-model="form.addressUpdateBy" placeholder="请输入更新者" />
        </el-form-item>
        <el-form-item label="更新时间" prop="addressUpdateTime">
          <el-date-picker clearable
            v-model="form.addressUpdateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标志" prop="addressDelFlag">
          <el-select v-model="form.addressDelFlag" placeholder="请选择删除标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认标志" prop="addressDefaultFlag">
          <el-select v-model="form.addressDefaultFlag" placeholder="请选择默认标志">
            <el-option
              v-for="dict in dict.type.service_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="addressRemark">
          <el-input v-model="form.addressRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAddress, getAddress, delAddress, addAddress, updateAddress } from "@/api/service/address";

export default {
  name: "Address",
  dicts: ['service_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务地址表格数据
      addressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeAddressCreateTime: [],
      // 备注时间范围
      daterangeAddressUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        addressUserid: null,
        addressInfo: null,
        addressDetail: null,
        addressPeople: null,
        addressPhone: null,
        addressCreateBy: null,
        addressCreateTime: null,
        addressUpdateBy: null,
        addressUpdateTime: null,
        addressDelFlag: null,
        addressDefaultFlag: null,
        addressRemark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务地址列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeAddressCreateTime && '' != this.daterangeAddressCreateTime) {
        this.queryParams.params["beginAddressCreateTime"] = this.daterangeAddressCreateTime[0];
        this.queryParams.params["endAddressCreateTime"] = this.daterangeAddressCreateTime[1];
      }
      if (null != this.daterangeAddressUpdateTime && '' != this.daterangeAddressUpdateTime) {
        this.queryParams.params["beginAddressUpdateTime"] = this.daterangeAddressUpdateTime[0];
        this.queryParams.params["endAddressUpdateTime"] = this.daterangeAddressUpdateTime[1];
      }
      listAddress(this.queryParams).then(response => {
        this.addressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        addressId: null,
        addressUserid: null,
        addressInfo: null,
        addressDetail: null,
        addressPeople: null,
        addressPhone: null,
        addressCreateBy: null,
        addressCreateTime: null,
        addressUpdateBy: null,
        addressUpdateTime: null,
        addressDelFlag: null,
        addressDefaultFlag: null,
        addressRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeAddressCreateTime = [];
      this.daterangeAddressUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.addressId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务地址";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const addressId = row.addressId || this.ids
      getAddress(addressId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务地址";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.addressId != null) {
            updateAddress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const addressIds = row.addressId || this.ids;
      this.$modal.confirm('是否确认删除服务地址编号为"' + addressIds + '"的数据项？').then(function() {
        return delAddress(addressIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('service/address/export', {
        ...this.queryParams
      }, `address_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
